<?php
/**
 * Created by PhpStorm.
 * User: Edward
 * Date: 2019/5/31
 * Time: 13:23
 */

namespace app\admin\controller;


use app\admin\BaseController;
use app\admin\model\ChinaArea;
use app\admin\model\ChinaCity;
use app\admin\model\ChinaProvince;
use app\lib\exception\NotFoundException;
use app\Validate\IdMustPositive;
use think\Request;

class Address extends BaseController {

    /**
     * 获取省份信息
     * @return \think\response\Json
     * @throws NotFoundException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function province(){
        $province = ChinaProvince::province()->toArray();
//        $province = (new ChinaProvince())->selectList([]);
        if(empty($province)){
            throw new NotFoundException([
                'msg'=>'暂无省份信息，请联系管理员添加'
            ]);
        }
        return writeJson('200',['province'=>$province]);
    }

    /**
     *  根据省份ID查寻下级城市
     * @param Request $request
     * @return \think\response\Json
     * @throws NotFoundException
     * @throws \app\lib\exception\ParameterException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function city(Request $request){
        (new IdMustPositive())->goCheck();
        $provinceId = $request->id;
        $city = ChinaCity::city($provinceId)->toArray();
        if(empty($city)){
            throw  new NotFoundException([
                'msg'=>'暂无城市信息，请联系管理员添加'
            ]);
        }
        return writeJson('200',['city'=>$city]);
    }

    public function area(Request $request){
        (new IdMustPositive())->goCheck();
        $cityId = $request->id;
        $area = ChinaArea::area($cityId)->toArray();
        if(empty($area)){
            throw  new NotFoundException([
                'msg'=>'暂无地区信息，请联系管理员'
            ]);
        }
        return writeJson('200',['area'=>$area]);
    }
}